#!/bin/bash
# Alist 保活配置脚本（实际路径：/root/alist）

# 检查root权限
if [ "$(id -u)" -ne 0 ]; then
  echo "请使用sudo或root权限运行"
    exit 1
    fi

    # 定义Alist目录和工作目录
    ALIST_DIR="/root"
    SERVICE_FILE="/etc/systemd/system/alist.service"

    # 检查目录存在性
    if [ ! -d "$ALIST_DIR" ]; then
      echo "目录 $ALIST_DIR 不存在，请确认路径"
        exit 1
        fi

        # 生成systemd服务文件
        cat > "$SERVICE_FILE" << EOF
        [Unit]
        Description=Alist File Manager
        After=network.target

        [Service]
        Type=simple
        ExecStart=$ALIST_DIR/alist server  # 调用/root/alist/alist文件
        Restart=always          # 失败时自动重启
        RestartSec=5s           # 重启间隔5秒
        WorkingDirectory=$ALIST_DIR  # 工作目录

        [Install]
        WantedBy=multi-user.target
        EOF

        # 重载配置并启动服务
        systemctl daemon-reload
        systemctl start alist
        systemctl enable alist

        # 验证状态
        status=$(systemctl status alist --no-pager | grep "active")
        if [[ $status == *"active (running)"* ]]; then
          echo "✅ Alist保活配置完成！"
            echo "访问 http://服务器IP:5214 管理"
              systemctl status alist --no-pager | head -n 2
              else
                echo "❌ 启动失败，查看日志：systemctl status alist"
                fi
                